<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.js"></script>

<body>
    <h1>
        HELLO WORLD!
    </h1>

    <input type="button" id="btn" value="下一份作业" />
    <!-- <input type="button" id="btn_save" value="保存已批改的作业" /> -->
    <input type="button" id="btn_refresh" value="刷新当前作业" />

    <label>总共有题目</label>
    <input type="number" id="input_all_problems_num">
    <label>做错的题目数量</label>
    <input type="number" id="input_wrong_answer_num">


    <div class="box">
        <iframe id=iframe_assignment src="/assignments/" frameborder="0" scrolling="no" width="100%" height="1000"
            align="left"> </iframe>
    </div>

    <!-- <div class="box">
        <iframe src="/answers/第一次作业.pdf" frameborder="0" scrolling="no" width="50%" height="950" align="right">
        </iframe>
    </div> -->

    <script>
        function nextProblem() {
            let s = document.getElementById('iframe_assignment').src;
            let position = s.search("/assignments/") + 13;
            let filename = decodeURI(s.substr(position));
            // let filename = (s.substr(position));
            let input_all_problems_num = document.getElementById('input_all_problems_num').value;
            let input_wrong_answer_num = document.getElementById('input_wrong_answer_num').value;
            document.getElementById('input_wrong_answer_num').value = "";

            if(filename.length != 0)
            {
                if(input_all_problems_num.length == 0 || input_wrong_answer_num.length == 0)
                {
                    alert("错误！请检查题目数量以及做错的题目数量是否已经输入!");
                    return;
                }
                if(Number(input_wrong_answer_num) > Number(input_all_problems_num))
                {
                    alert("错误！做错的题目数量超过了总题目数量！");
                    return;
                }
            }
            $.ajax({
                type: "GET",
                url: "/assignments/next?all_problems_num=" + input_all_problems_num + "&wrong_answer_num=" + input_wrong_answer_num + "&filename=" + filename,
                dataType: "json",
                success: function (data) {
                    document.getElementById('iframe_assignment').src = '/assignments/' + data.filename;
                    if(data.filename.length == 0)
                    {
                        alert("所有的作业都已经批改完毕！");
                    }
                },
                error: function (jqXHR) {
                    alert("发生错误：" + jqXHR.status);
                },
            });
        }
        btn.onclick = nextProblem;

        // function save() {
        //     console.log('save function');
        //     let s = document.getElementById('iframe_assignment').src;
        //     let position = s.search("/assignments/") + 13;
        //     let filename = s.substr(position);
        //     console.log(decodeURI(filename)); // decodeURI 用于解析中文


        //     var frameObj = document.getElementById('iframe_assignment');
        //     frameObj.focus();
        //     frameObj.contentWindow.print();
        // }
        // btn_save.onclick = save;

        function refresh() {
            document.getElementById('input_wrong_answer_num').value = "";
            document.getElementById('iframe_assignment').src += '';
        }
        btn_refresh.onclick = refresh;
    </script>

</body>

</html>